package com.gitee.feizns.quickstart.jackson.module;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.gitee.feizns.dynamic.time.Times;

import java.io.IOException;
import java.time.temporal.TemporalAccessor;

/**
 * json时间串并转换器
 * @author feizns
 * @since 2023/05/30
 */
public class Java8TimeDeserializer<T extends TemporalAccessor> extends JsonDeserializer<T> {

    /**
     * 目标类型Deserializer
     */
    private final Class<T> targetType;

    /**
     * java8时间串并转换器
     * @param targetType 圆盾类型
     */
    public Java8TimeDeserializer(Class<T> targetType) {
        this.targetType = targetType;
    }

    @Override
    public T deserialize(JsonParser p, DeserializationContext cxt) throws IOException {
        return Times.parse(p.getValueAsString(), targetType);
    }

}
